package cn.psvmc.cxzapi.controller.customer;

import cn.psvmc.cxzapi.model.common.*;
import cn.psvmc.cxzapi.model.customer.TCustomerAuth;
import cn.psvmc.cxzapi.model.order.*;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.customer.TCustomerOrderService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ywx
 * @className TCustomerOrderController
 * @description
 * @date 2024/9/3 22:17
 **/
@RestController
@RequestMapping("/mpapi/customer_order")
public class TCustomerOrderController {
    @Resource
    private TCustomerOrderService customerOrderService;

    /**
     * @Description 订单列表
     * @Date 2024/8/27 22:40
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_order")
    public PageResult2 listOrder(@RequestBody OrderEntity entity) {
        Page<TOrder> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(customerOrderService.listOrder(queryPage, entity));
    }

    /**
     * @Description 买断申请
     * @Date 2024/8/27 15:02
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_buyout")
    public Result addBuyout(@RequestBody DetailEntity entity) {
        Integer orderId = entity.getId();
        ToolUtils.validation(new Object[]{
                orderId, "订单id", 1
        });
        customerOrderService.addBuyout(orderId);
        return ResultGenerator.genSuccessResult("申请成功！");
    }

    /**
     * @Description 我要续租
     * @Date 2024/8/30 23:45
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/renew_lease")
    public Result renewLease(@RequestBody OrderBillEntity bill) {
        ToolUtils.validation(new Object[]{
                bill.getOrderId(), "订单id", 1
                , bill.getEndTime(), "结束日期", 1
        });
        customerOrderService.renewLease(bill);
        return ResultGenerator.genSuccessResult("申请成功！");
    }

    /**
     * @Description 获取续租实付金额
     * @Date 2024/9/7 16:20
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_renew_lease_price")
    public Result getRenewLeasePrice(@RequestBody OrderBillEntity bill) {
        ToolUtils.validation(new Object[]{
                bill.getOrderId(), "订单id", 1
                , bill.getEndTime(), "结束日期", 1
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getRenewLeasePrice(bill));
    }

    /**
     * @Description 订单详情
     * @Date 2024/9/4 22:45
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_order")
    public Result detailOrder(@RequestBody OrderBillEntity entity) {
        Integer orderId = entity.getOrderId();
        ToolUtils.validation(new Object[]{
                orderId, "订单id", 1
        });
        return ResultGenerator.genSuccessResult("获取成功！", customerOrderService.detailOrder(entity));
    }

    /**
     * @Description 快捷下单成功详情
     * @Date 2025/1/16 22:41
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_order_by_code")
    public Result detailOrderByCode(@RequestBody OrderBillEntity entity) {
        String orderCode = entity.getOrderCode();
        ToolUtils.validation(new Object[]{
                orderCode, "订单号", 2
        });
        return ResultGenerator.genSuccessResult("获取成功！", customerOrderService.detailOrderByCode(orderCode));
    }

    /**
     * @Description 归还
     * @Date 2024/9/19 12:44
     * @Author YWX
     * @Param [orderDelivery]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/send_back")
    public Result sendBack(@RequestBody TOrderDelivery orderDelivery) {
        ToolUtils.validation(new Object[]{
                orderDelivery.getOrderCode(), "订单号", 2
                , orderDelivery.getTrackingCode(), "快递单号", 2
                , orderDelivery.getDeliveryType(), "快递公司类型", 2
        });
        customerOrderService.sendBack(orderDelivery);
        return ResultGenerator.genSuccessResult("归还成功！");
    }

    /**
     * @Description 物流信息
     * @Date 2024/9/21 16:36
     * @Author YWX
     * @Param [tOrderDelivery]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_delivery")
    public Result detailDelivery(@RequestBody TOrderDelivery tOrderDelivery) {
        String orderCode = tOrderDelivery.getOrderCode();
        Integer type = tOrderDelivery.getType();
        ToolUtils.validation(new Object[]{
                orderCode, "订单号", 2
                , type, "快递类型", 1
        });
        return ResultGenerator.genSuccessResult("查询成功", customerOrderService.detailDelivery(orderCode, type));
    }

    /**
     * @Description 获取订单押金支付状态
     * @Date 2024/9/21 16:07
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_deposit_status")
    public Result getDepositStatus(@RequestBody TOrderBill bill) {
        Integer orderId = bill.getOrderId();
        ToolUtils.validation(new Object[]{
                orderId, "订单id", 1
        });
        return ResultGenerator.genSuccessResult("获取成功！", customerOrderService.getDepositStatus(orderId));
    }

    /**
     * @Description 取消订单
     * @Date 2024/9/24 22:14
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/order_cancel")
    public Result orderCancel(@RequestBody TOrderBill bill) {
        Integer orderId = bill.getOrderId();
        ToolUtils.validation(new Object[]{orderId, "订单id", 1});
        customerOrderService.orderCancel(orderId);
        return ResultGenerator.genSuccessResult("取消成功！");
    }

    /**
     * @Description 获取身份认证地址
     * @Date 2024/9/25 22:35
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_certify_url")
    public Result getCertifyUrl(@RequestBody TOrderBill bill) {
        Integer orderId = bill.getOrderId();
        ToolUtils.validation(new Object[]{orderId, "订单id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getCertifyUrl(orderId));
    }

    /**
     * @Description 根据姓名身份证号获取身份认证地址
     * @Date 2024/9/27 22:01
     * @Author YWX
     * @Param [customer]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_certify_url2")
    public Result getCertifyUrl2(@RequestBody TCustomerAuth auth) {
        ToolUtils.validation(new Object[]{
                auth.getOrderId(), "订单id", 1
                , auth.getInRealName(), "输入真实姓名", 2
                , auth.getInIdNumber(), "输入身份证号码", 2
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getCertifyUrl2(auth));
    }

    /**
     * @Description 查询身份认证结果
     * @Date 2024/9/25 22:36
     * @Author YWX
     * @Param [order]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_certify_result")
    public Result getCertifyResult(@RequestBody TOrder order) {
        String certifyId = order.getCertifyId();
        ToolUtils.validation(new Object[]{certifyId, "认证标识", 2});
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getCertifyResult(certifyId));
    }

    /**
     * @Description 获取订单认证信息
     * @Date 2024/9/25 23:33
     * @Author YWX
     * @Param [order]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_order_auth")
    public Result getOrderAuth(@RequestBody OrderBillEntity entity) {
        ToolUtils.validation(new Object[]{entity.getOrderId(), "订单id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getOrderAuth(entity));
    }

    /**
     * @Description 获取订单服务信息
     * @Date 2024/9/27 13:07
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_order_service")
    public Result getOrderService(@RequestBody OrderBillEntity entity) {
        Integer orderId = entity.getOrderId();
        ToolUtils.validation(new Object[]{orderId, "订单id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getOrderService(orderId));
    }

    /**
     * @Description 获取商家服务信息
     * @Date 2024/11/9 22:26
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_store_service")
    public Result getStoreService(@RequestBody OrderEntity entity) {
        Integer storeid = entity.getStoreid();
        ToolUtils.validation(new Object[]{storeid, "商家id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getStoreService(storeid));
    }

    /**
     * @Description 订单协议列表
     * @Date 2024/10/4 23:12
     * @Author YWX
     * @Param [contract]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_contract")
    public Result listContract(@RequestBody OrderBillEntity contract) {
        Integer orderId = contract.getOrderId();
        ToolUtils.validation(new Object[]{
                orderId, "订单id", 1
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.listContract(orderId));
    }

    /**
     * @Description 根据订单号获取账单详情
     * @Date 2024/10/9 21:57
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_order_bill_by_code")
    public Result detailOrderBillByCode(@RequestBody OrderBillEntity entity) {
        ToolUtils.validation(new Object[]{
                entity.getOrderCode(), "订单号", 2
        });
        return ResultGenerator.genSuccessResult("获取成功！", customerOrderService.detailOrderBill(entity));
    }

    /**
     * @Description 根据订单号获取订单认证信息
     * @Date 2024/10/9 22:01
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_order_auth_by_code")
    public Result getOrderAuthByCode(@RequestBody OrderBillEntity entity) {
        ToolUtils.validation(new Object[]{entity.getOrderCode(), "订单号", 2});
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getOrderAuth(entity));
    }

    /**
     * @Description 根据订单号获取身份认证地址
     * @Date 2024/10/9 22:08
     * @Author YWX
     * @Param [auth]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_certify_url2_by_code")
    public Result getCertifyUrl2ByCode(@RequestBody TCustomerAuth auth) {
        ToolUtils.validation(new Object[]{
                auth.getOrderCode(), "订单号", 2
                , auth.getInRealName(), "输入真实姓名", 2
                , auth.getInIdNumber(), "输入身份证号码", 2
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getCertifyUrl2(auth));
    }

    /**
     * @Description 申请取消订单
     * @Date 2024/10/9 22:40
     * @Author YWX
     * @Param [order]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/apply_for_cancel")
    public Result applyForCancel(@RequestBody TOrder order) {
        ToolUtils.validation(new Object[]{
                order.getOrderCode(), "订单号", 2
                , order.getCancelCause(), "申请取消原因", 1
        });
        if (order.getCancelCause().equals(110) && ToolUtils.isEmpty(order.getCancelComm())) {
            return ResultGenerator.genFailResult("申请取消备注不能为空！");
        }
        customerOrderService.applyForCancel(order);
        return ResultGenerator.genSuccessResult("申请成功！");
    }

    /**
     * @Description 获取账单支付信息
     * @Date 2024/10/12 13:00
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_bill_pay")
    public Result getBillPay(@RequestBody TOrderBill bill) {
        ToolUtils.validation(new Object[]{
                bill.getObid(), "订单账单id", 1
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getBillPay(bill));
    }

    /**
     * @Description 归还详情
     * @Date 2024/10/22 23:07
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_return")
    public Result detailReturn(@RequestBody OrderBillEntity entity) {
        String orderCode = entity.getOrderCode();
        ToolUtils.validation(new Object[]{
                orderCode, "订单号", 2
        });
        return ResultGenerator.genSuccessResult("获取成功！", customerOrderService.detailReturn(orderCode));
    }

    /**
     * @Description 确认收货
     * @Date 2024/10/25 17:03
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/confirm_receipt")
    public Result confirmReceipt(@RequestBody OrderBillEntity entity) {
        String orderCode = entity.getOrderCode();
        ToolUtils.validation(new Object[]{
                orderCode, "订单号", 2
        });
        customerOrderService.confirmReceipt(orderCode);
        return ResultGenerator.genSuccessResult("确认成功！");
    }

    /**
     * @Description 获取订单状态数量
     * @Date 2024/10/28 23:49
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_status_num")
    public Result getStatusNum() {
        return ResultGenerator.genSuccessResult("查询成功！", customerOrderService.getStatusNum());
    }
}
